package ru.sstu.renju.players;

import java.util.Random;

import ru.sstu.renju.core.CellType;
import ru.sstu.renju.core.Game;
import ru.sstu.renju.core.Move;
import ru.sstu.renju.core.Player;

/**
 * <code>RandomPlayer</code> class represents {@link Player} doing random moves.
 *
 * @author denis_murashev
 * @since Renju 1.0
 */
public class RandomPlayer implements Player {

	private static final Random RANDOM = new Random();

	@Override
	public String toString() {
		return "Random";
	}

	@Override
	public Move doMove(Game game, CellType type) {
		int freeCells = game.getSize() * game.getSize() - game.getMoveCount();
		int index = RANDOM.nextInt(freeCells);
		for (int i = 0; i < game.getSize(); i++) {
			for (int j = 0; j < game.getSize(); j++) {
				if (game.isFreeCell(i, j)) {
					if (index == 0) {
						return new Move(game, i, j);
					}
					index--;
				}
			}
		}
		return null;
	}
}
